package com.gin.kotlin.no_gay_alliance.activity

import android.content.Context
import android.content.Intent
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.adapter.reply.AttachmentAdapter
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.databinding.ActivityAttachmentBinding
import com.gin.kotlin.no_gay_alliance.model.AttachmentModel

class AttachmentActivity : AbstractBindingActivity<ActivityAttachmentBinding>() {

    private lateinit var adapter: AttachmentAdapter
    private lateinit var attachments: ArrayList<AttachmentModel>

    override fun onCreate() {
        val b = this.b ?: return
        this.adapter = AttachmentAdapter(this, handler!!)
        b.recyclerView.adapter = this.adapter
        this.adapter.replaceData(attachments)
    }

    override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, menuInflater)
        menuInflater.inflate(R.menu.attachment, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.mi_select_all -> adapter.selectAll()
            R.id.mi_unselect_all -> adapter.unselectAll()
            R.id.mi_download -> adapter.downloadSelected()
        }

        return super.onOptionsItemSelected(item)
    }

    override fun handleIntent(intent: Intent) {
        this.attachments = parseArgumentList(intent, AttachmentModel::class.java.simpleName, AttachmentModel::class.java)
    }


    override fun onResume() {
        super.onResume()
        setToolbarTitle("附件列表")
    }

    companion object {
        fun startActivity(context: Context, attachments: List<AttachmentModel>) = context.startActivity(Intent(context, AttachmentActivity::class.java).also {
            it.putParcelableArrayListExtra(
                AttachmentModel::class.java.simpleName,
                arrayListOf(* attachments.toTypedArray())
            )
        })
    }
}